package com.cypher.encryptionanddecryption.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.cypher.encryptionanddecryption.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;

/**
 *  蓝牙信息列表适配器
 */
public class BluetoothListAdapter extends BaseAdapter implements View.OnClickListener {

    /**
     * 点击事件回调函数
     */
    public interface Callback{

        /**
         * 点击事件
         * @param v
         */
        void onClick(View v);

    }

    private Callback callback;
    public Callback getCallback() {
        return callback;
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    // 存放蓝牙设备信息的集合
    private List<Map<String, Object>> devices;
    /**
     * 上下文对象
     */
    private Context context;

    public List<Map<String, Object>> getDevices() {
        return devices;
    }

    public void setDevices(List<Map<String, Object>> devices) {
        this.devices = devices;
    }

    public BluetoothListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return devices.size();
    }

    @Override
    public Object getItem(int position) {
        return devices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void onClick(View v) {
        callback.onClick(v);
    }

    class ViewHolder {
        TextView bluetoothName;
        TextView bluetoothAddress;
        Button connectBtn;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.fragment_bluetooth_list_item, parent, false);
            viewHolder.bluetoothName = convertView.findViewById(R.id.bluetooth_name);
            viewHolder.bluetoothAddress = convertView.findViewById(R.id.bluetooth_address);
            viewHolder.connectBtn = convertView.findViewById(R.id.connect_btn);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.bluetoothName.setText(devices.get(position).get("name").toString());
        viewHolder.bluetoothAddress.setText(devices.get(position).get("address").toString());

        // 连接蓝牙按钮监听事件
        viewHolder.connectBtn.setOnClickListener(this);
        Map<String, Object> tapMap = new HashMap<String, Object>();
        tapMap.put("position", position);
        // 默认状态为可以连接的状态
        tapMap.put("state", "connect");
        viewHolder.connectBtn.setTag(tapMap);
        return convertView;
    }
}
